package com.qf.service.impl;

import com.qf.dao.UserDao;
import com.qf.dao.impl.UserDaoImpl;
import com.qf.pojo.Admin;
import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.utils.MD5Utils;
import com.qf.utils.MailUtils;

import java.sql.SQLException;


public class UserServiceImp implements UserService {


    @Override
    public boolean findUserByName(String username) {
        UserDao userDao = new UserDaoImpl ();

        try {
            User user = userDao.selectUserByName ( username );

            if (user==null){
                return true;
            }

        } catch (SQLException e) {
            e.printStackTrace ( );
        }return false;

    }

    @Override
    public void updateUser(User user) {
        try {
            UserDao userDao = new UserDaoImpl ();
            userDao.updateUser (user);
        } catch (SQLException e) {
            e.printStackTrace ( );
        }
    }

    @Override
    public void addUser(User user) {
        try {
            UserDao userDao = new UserDaoImpl ();
            userDao.addUserSelect(user);
        } catch (SQLException e) {
        }
      //邮件激活  发送邮件
       // 参数一  收件地址
       //参数二   正文
       //参数三   标题
        MailUtils.sendMail ( user.getEmail (),"欢迎激活会员,<a href='http://localhost:8080/aaa_war/userActive?code="+user.getCode ()+"'>请激活</a>","邮箱激活");

    }
//激活码查找用户
    @Override
    public User getUserByCode(String code) {

        try {
            UserDao userDao = new UserDaoImpl ( );
            User user = userDao.getUserByCodeSelect ( code );

            if (user==null){
                return null;
            }
            if (user!=null){

                user.setState ( 1 );
                userDao.updateUser ( user );
                return user;

            }
        } catch (SQLException e) {
            e.printStackTrace ( );
        }return null;


    }

    @Override
    public User findUserByNameAndPassword(String username , String password) {
        try {
            UserDao userDao = new UserDaoImpl ();
            User user =userDao.findUserByNameAndPassword(username,password);
            return user;
        } catch (SQLException e) {
            e.printStackTrace ( );
        }return null;


    }

}
